//
//  TMyProfileController.m
//  TUIKit
//
//  Created by annidyfeng on 2019/3/11.
//  Copyright © 2019年 kennethmiao. All rights reserved.
//

#import "TMyProfileController.h"
#import "TSettingController.h"
#import "TPersonalCommonCell.h"
#import "TKeyValueCell.h"
#import "TButtonCell.h"
#import "THeader.h"
#import "TAlertView.h"
//#import "IMMessageExt.h"
#import "TIMFriendshipManager.h"
#import "TRichMenuCell.h"
#import "TRichMenuCellData.h"
#import "UIView+MMLayout.h"
#import "TEditInfoViewController.h"

@interface TMyProfileController ()
@property NSMutableArray<TRichMenuCellData *> *dataArray;
@property TIMUserProfile *inProfile;
@property BOOL isSelfProfile;
@end

@implementation TMyProfileController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViews];
}

- (void)setupViews
{
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.backgroundColor = TSettingController_Background_Color;


//    NSArray *n = @[@"1519783020798103",@"1509533457234589",@"1518461609164254",@"1517581756219348",@"1511170161347908",@"1517387328959016",@"1518495275409874",@"1522044264984969",@"1516936302403472",@"1521611664473386",@"1518335552528533",@"1518584442417525",@"1516891016279275",@"1516812514832029",@"1520827559550782",@"1499826375865874",@"1515504675395743",@"1516168877043976",@"1517302469059090",@"1512550998423317",@"1502353092357307",@"1502349662339070",@"1504169650432326",@"1520494715495446",@"1495125273488549",@"1518085625307974",@"1503547715143667",@"1513489362487181",@"1520932411937392",@"1518341490719076",@"1513487414007521",@"1512445240644131",@"1522224792322409",@"1522989907432284",@"1518171062093059",@"1521686680355793",@"1520320377275236",@"1511846502943238",@"1510316203211437",@"1510313401117548",@"1523021994010902",@"1511343755162550",@"1510325470023129",@"1519113543063085",@"1510313103662362",@"1510314444598120",@"1518929072167310",@"1521118524916397",@"1508400676635437",@"1522767583256556",@"1519611711701831",@"1520955822190832",@"1510313675891358",@"1522311120309201",@"1522755001744627",@"1522725875829972",@"1510315568239766",@"1510313829026233",@"1523536890136693",@"1510315672985294",@"1510323944158942",@"1510316357159425",@"1522385661547094",@"1522236492563596",@"1520334837866041",@"1522373944054839",@"1522959387814549",@"1512636411526776",@"1521727611461979",@"1521595402083199",@"1510313425490257",@"1523191702630747",@"1510314225931176",@"1510314317621620",@"1510317112323801",@"1522149154732907",@"1519737330321875",@"1510323798550727",@"1510316295599428",@"1510314577121471",@"1518570661352703",@"1512716140514134",@"1510326796436894",@"1523365573308643",@"1519269464255342",@"1510315570879389",@"1510317177161561",@"1521689254791828",@"1510327126833538",@"1521640083440275",@"1522304803863923",@"1510318254059305",@"1520047546910165",@"1522379551529527",@"1522828523641356",@"1521699421771636",@"1510315798287588",@"1522681520480081",@"1521785692621639",@"1510328851358071",@"1510323624551959",@"1510329372707226",@"1510329276939164",@"1510321310236102",@"1510324105419185",@"1510325242962033",@"1506540480841417",@"1510326635802823",@"1522158325818349",@"1523512225580739",@"1510331010766667",@"1510322250225945",@"1510320853439648",@"1510323984575851",@"1518945597602751",@"1510321656666199",@"1523421132564711",@"1523604931104941",@"1521090759622837",@"1510328614975008",@"1518945626970910",@"1510320884310678",@"1510331343255576",@"1510321872978773",@"1510320414380693",@"1510324636166210",@"1510325524990628",@"1510323733014533",@"1510328046973014",@"1522903423510136",@"1518773917445199",@"1510326081109730",@"1510324045622543",@"1510327603048618",@"1523072108742751",@"1522998931329010",@"1518927780484267",@"1510327808247600",@"1523412994373182",@"1510321381789975",@"1510318636749396",@"1523414290632458",@"1523021484277785",@"1510325466894694",@"1506249221863273",@"1518769262304494",@"1510316606880133",@"1522201744208743",@"1523241010665146",@"1518692763515101",@"1523242801476411",@"1510321463648894",@"1523517456920208",@"1523251865208085",@"1523579158777050",@"1522050527134606",@"1510327816613455",@"1523025341833636",@"1523273922859585",@"1518742722165097",@"1518770921703749",@"1510321412282793",@"1510322332003045",@"1510328642139584",@"1522573037791898",@"1523426077225697",@"1522054265571543",@"1523519534925565",@"1523584260592671",@"1518756935958285",@"1510332560540350",@"1510323771491089",@"1510321620913863",@"1510324704014428",@"1510328022271897",@"1510321721519504",@"1523519934926385",@"1523519988012423",@"1523520147336901",@"1522055849163399",@"1518928478551877",@"1523520482375766",@"1510318553414438",@"1510319809153027",@"1522576636353228",@"1522496602340659",@"1519615823031110",@"1519273084818367",@"1510327743822924",@"1522317365687052",@"1519898836817889",@"1510322196666823",@"1518696272779934",@"1523523869568430",@"1522076299921760",@"1518754385264085",@"1518745421011980",@"1523525151834008",@"1522520602036633",@"1522064998710754",@"1510323843589176",@"1510327692415766",@"1522317341583585",@"1518872905342980",@"1510324953306723",@"1523444119499524",@"1523598951564371",@"1510320762832787",@"1523517464723305",@"1521931226677610",@"1518709619991602",@"1523344320880585",@"1523600781060947",@"1522070280060951",@"1518933559417118",@"1523528288555903",@"1523323049923013",@"1523528771146682",@"1526805336401608",@"1526806705499937",@"1526811498892056",@"1526817013081479",@"1526819243123290",@"1526848043408922",@"1526860219055880",@"1526863935231576",@"1526880258693419",@"1526886750999377",@"1526904677572628",@"1526913262219766",@"1526963289872731",@"1526970647447764",@"1526977267585348",@"1526981986487296",@"1526994224767854",@"1527000949623633",@"1527002443060653",@"1527045527818880",@"1527048690608593",@"1527049585536878",@"1527050593906479",@"1527050766301054",@"1527058455727789",@"1527067553179495",@"1527083754791278",@"1527084028358409",@"1527093803003432",@"1527099220510914",@"1527100362025779",@"1527112017421650",@"1527135478362348",@"1527138582413364",@"1527143882171333",@"1527147959149205",@"1527148743470426",@"1527153181307375",@"1527164513573710",@"1527165349273448",@"1527170060665392",@"1527172240637282",@"1527196300750308",@"1527219315056764",@"1527221144987757",@"1527224032847276",@"1527224558199279",@"1527233654619136",@"1527238263759277",@"1527240068135017",@"1527240941017520",@"1527245367834678",@"1527245505615917",@"1527246996007935",@"1527248878625559",@"1527250332846008",@"1527251955819282",@"1527252298828042",@"1527252496750900",@"1527252638960282",@"1527283018244435",@"1527295663787052",@"1527300712762252",@"1527301487068735",@"1527307090975738",@"1527308421035653",@"1527312281346202",@"1527318896153618",@"1527319285137597",@"1527325329421489",@"1527336522131173",@"1527361268057725",@"1527368608446885",@"1527386700641252",@"1527386895055463",@"1527390441245229",@"1527409997460304",@"1527410121363771",@"1527419243629081",@"1527430044424313",@"1527515893786556",@"1527518396993913",@"1527521809399130",@"1527555821970177",@"1527569088608512",@"1527593371654367",@"1527601685133961",@"1527603736778671",@"1527605250393939",@"1527609551065263",@"1527611373818972",@"1527611734087494",@"1527628367896446",@"1527651306498542",@"1527661352867685",@"1527664405978108",@"1527681173752406",@"1527682311638507",@"1527683462259188",@"1527686109035848",@"1527687387301498",@"1527688046062593",@"1527716923401092",@"1527729416856043",@"1527732252098781",@"1527738856731644",@"1527748805144457",@"1527757906162179",@"1527764565935081",@"1527765360769114",@"1528732209719155",@"1528776273020562",@"1528783300764493",@"1528791674616684",@"1528796421978651",@"1528798577521769",@"1528807956563182",@"1528809129944925",@"1528827577443804",@"1528847540109179",@"1528866617003391",@"1528867345954101",@"1528869627862529",@"1528873528318064",@"1528875130905335",@"1528877369186139",@"1528881348459571",@"1528883202298056",@"1528883829318036",@"1528885483757903",@"1528887654968997",@"1528888434099088",@"1528888469264037",@"1528888635240838",@"1528888814488181",@"1528888942380520",@"1528889281992920",@"1528891119942104",@"1528891280029146",@"1528891506421029",@"1528891616583328",@"1528892781219265",@"1528895548880806",@"1528895857060744",@"1528896344782938",@"1528896415825794",@"1528896436693998",@"1528896526292592",@"1528896566080674",@"1528896575702360",@"1528896581194986",@"1528896590938951",@"1528896591806834",@"1528896640389498",@"1528896780424498",@"1528896856054152",@"1528896887475168",@"1528896888736756",@"1528896964498258",@"1528897032584361",@"1528897085536594",@"1528897128145297",@"1528897138948663",@"1528897198488878",@"1528897570726442",@"1528899237967370",@"1528906605997379",@"1528907856487425",@"1528928765811230",@"1528947516472530",@"1528958935361244",@"1528960782884291",@"1528968207198572",@"1528971782768580",@"1528972187371467",@"1528972319618090",@"1528975614267542",@"1528981702335264",@"1528999131078870",@"1529032774006883",@"1529043647454683",@"1529046776218523",@"1529136961066791",@"1529137082496975",@"1529138248451463",@"1529145255553324",@"1529147085428078",@"1529151557787504",@"1529152779836358",@"1529157049210546",@"1529157800239131",@"1529167723929975",@"1529193205727315",@"1529196431280675",@"1529204562231768",@"1529207090432692",@"1529216613725569",@"1529290420570370",@"1529291420085747",@"1529293502688410",@"1529295334656377",@"1529301761951537",@"1529301800167371",@"1529302214490962",@"1529302419951430",@"1529302532891262",@"1529302925347369",@"1529311986174524",@"1529314555766660",@"1529315628009469",@"1529333835749553",@"1528449533627584",@"1529385011729912",@"1529388628626665",@"1529397774745486",@"1529405873412548",@"1529407649675111",@"1529416465038108",@"1529419708966197",@"1529460130698212",@"1534685737569443",@"1534686146099030",@"1534686346721992",@"1534686629642351",@"1534686641473468",@"1534687327394117",@"1534688094598459",@"1534688192762723",@"1534688219836484",@"1534689858815440",@"1534720265480375",@"1534720325482286",@"1534722993973602",@"1534756851110139",@"1534776315870359",@"1534799000341076",@"1534824539500956",@"1534831617138279",@"1534852507719422",@"1534853737955057",@"1534902642379095",@"1534909694885969",@"1534914722091666",@"1534919584530902",@"1534921304100397",@"1534933158398277",@"1534934907937175",@"1534945666555877",@"1534948150644876",@"1534949251442687",@"1534949257534869",@"1534949857216057",@"1534950658855601",@"1534951164191948",@"1534951171572160",@"1534951242848113",@"1534951378737492",@"1534976968282728",@"1534979357788114",@"1534985201352489",@"1534995757795125",@"1535004873403763",@"1535005595607386",@"1535006679731154",@"1535006782273754",@"1535009061418699",@"1535010440623225",@"1534921474876089",@"1534930244781893",@"1534943526220166",@"1534945224620013",@"1534950218760293",@"1534950262208588",@"1534950954372183",@"1534951303522470",@"1534951318363931",@"1534951378819037",@"1534953985247708",@"1534954318675700",@"1534960070203965",@"1534971968674137",@"1534993802204807",@"1535004902310401",@"1535006981413855",@"1535024769114742",@"1535026532704218",@"1535028132356510",@"1535028636543367",@"1535031787339786",@"1535037686004868"];
//    
//    [[TIMFriendshipManager sharedInstance] getUsersProfile:n forceUpdate:YES succ:^(NSArray<TIMUserProfile *> *profiles) {
//
//    } fail:nil];
    
    [[TIMFriendshipManager sharedInstance] getUsersProfile:@[self.identifier] forceUpdate:YES succ:^(NSArray<TIMUserProfile *> *profiles) {
        self.inProfile = profiles.firstObject;
        [self setupData];
    } fail:nil];
}

- (void)setupData
{
    _dataArray = [NSMutableArray array];
    
    self.isSelfProfile = [self.inProfile.identifier isEqualToString:[[TIMManager sharedInstance] getLoginUser]];
    
    if (self.isSelfProfile)
    {
        [self setupSelfData];
    } else {
        [self setupUserData];
    }
}

- (void)setupSelfData
{
    TRichMenuCellData *uid = [[TRichMenuCellData alloc] init];
    uid.type = ERichCell_Text;
    uid.desc = @"帐号ID";
    uid.value = self.inProfile.identifier;
    [_dataArray addObject:uid];
    
    TRichMenuCellData *remark = [[TRichMenuCellData alloc] init];
    remark.type = ERichCell_TextNext;
    remark.desc = @"昵称";
    remark.value = self.inProfile.nickname;
    @m_weakify(self)
    remark.action = ^(TRichMenuCellData *menu, TRichMenuCell *cell) {
        @m_strongify(self)
        TEditInfoViewController *editVc = [[TEditInfoViewController alloc] initWithText:menu.value];
        editVc.title = @"修改昵称";
        editVc.completion = ^(TEditInfoViewController *sender, BOOL isFinished) {
            if (isFinished) {
                NSString *text = sender.inputTextField.text;
                self.inProfile.nickname = text;
                [[TIMFriendshipManager sharedInstance] modifySelfProfile:@{TIMProfileTypeKey_Nick:text
                                                                           } succ:^{
                    
                } fail:^(int code, NSString *msg) {
                    
                }];
                [self setupData];
            }
        };
        [self.navigationController pushViewController:editVc animated:YES];
    };

    
    [_dataArray addObject:remark];
    [self.tableView reloadData];
}

- (void)setupUserData
{
    TRichMenuCellData *uid = [[TRichMenuCellData alloc] init];
    uid.type = ERichCell_Text;
    uid.desc = @"帐号ID";
    uid.value = self.inProfile.identifier;
    [_dataArray addObject:uid];
    
    TRichMenuCellData *remark = [[TRichMenuCellData alloc] init];
    remark.type = ERichCell_Text;
    remark.desc = @"昵称";
    remark.value = self.inProfile.nickname;
    
    [_dataArray addObject:remark];
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TRichMenuCellData *data = _dataArray[indexPath.row];
    return [TRichMenuCell heightOf:data];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    TRichMenuCellData *data = _dataArray[indexPath.row];
    [data.assignCell doAction];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TRichMenuCellData *data = _dataArray[indexPath.row];
    
    TRichMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:data.reuseIndentifier];
    if(!cell){
        cell = [[TRichMenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:data.reuseIndentifier];
    }
    [cell setData:data];
    return cell;
}


@end
